package com.demo2do.boyle.backend.web.controller.aduser;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.aduser.AduserService;
import com.demo2do.boyle.common.entity.AuditStatus;
import com.demo2do.boyle.common.entity.aduser.Aduser;
import com.demo2do.boyle.common.entity.aduser.AduserLevel;
import com.demo2do.boyle.common.entity.aduser.transaction.AduserTransaction;
import com.demo2do.core.entity.Result;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Preloading;

/**
 * 
 * @author mohanlan leo
 *
 */
@Controller
@RequestMapping("/adusers")
@MenuSetting("menu-aduser")
public class AduserController {
	
	@Autowired
	private AduserService aduserService;
    
	/**
     * list all users
     * 
     * @return
     */
    @RequestMapping("")
    @MenuSetting("submenu-aduser-list")
    public ModelAndView list(Page page) {
        
    	ModelAndView modelAndView = new ModelAndView("aduser/aduser-list");
    	
    	int count = aduserService.count(Aduser.class);
    	modelAndView.addObject("count", count);
    	
    	if(count > 0) {
    		List<Aduser> adusers = aduserService.listByPage(Aduser.class, page);
        	modelAndView.addObject("adusers", adusers);
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @return
     */
    @RequestMapping("/search")
    public String search(){
        return "aduser/aduser-search";
    }
    
    /**
     * do search
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-aduser-list")
    public ModelAndView onSearch(Page page, HttpServletRequest request,
    							@RequestParam(value = "userName", required = false) String userName,
    							@RequestParam(value = "mobile", required = false) String mobile,
    							@RequestParam(value = "status", required = false) AuditStatus status,
    							@Preloading(value = "levelId", type = Integer.class) AduserLevel aduserLevel) {
    	
        ModelAndView modelAndView = new ModelAndView("aduser/aduser-list");
        
        Filter filter = new Filter().addEquals("disabled", false)
        							.addLike("profile.userName", userName)
        							.addLike("mobile", mobile)
        							.addEquals("profile.aduserLevel.id", aduserLevel.getId())
        							.addEquals("status", status);
        
        int count = aduserService.count(Aduser.class, filter);
        modelAndView.addObject("count", count);
        
        if(count > 0) {
        	List<Aduser> adusers = aduserService.list(Aduser.class, filter, page);
        	modelAndView.addObject("adusers", adusers);
        }
        
        //handler query string
        String queryString = request.getQueryString();
        Map<String,String> queries = StringUtils.parseQueryString(queryString);
        if(queries.containsKey("page")) {
        	queries.remove("page");
        }
        
        modelAndView.addObject("queryString", StringUtils.toQueryString(queries));
        
        return modelAndView;
    }
    
    /**
     * view aduser
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @MenuSetting("submenu-aduser-list")
    public ModelAndView view(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("aduser/aduser-view");
        Aduser aduser = aduserService.load(Aduser.class, id);
        List<AduserTransaction> aduserTransactions= aduserService.listTransactions(aduser);
       
        modelAndView.addObject("aduser", aduser);
        modelAndView.addObject("aduserTransactions", aduserTransactions);
        return modelAndView;
    }
    
    /**
     * prepare to create a new aduser
     * 
     * @return the create view
     */
    @RequestMapping("/create")
    public ModelAndView create() {
    	ModelAndView modelAndView = new ModelAndView("aduser/aduser-create");
    	return modelAndView;
    }
    
    
    /**
     * do create aduser
     * 
     * @param aduser	
     * @return the result
     */
    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody String onCreate(Aduser aduser) {
    	
    	Result result = new Result();
    	
    	aduserService.createAduser(aduser);
    	
    	result.setCode("0");
    	
    	return JsonUtils.toJsonString(result);
    	
    }
    
}
